<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;


use App\Common\Logger;
use App\Logic\WXOrderLogic;
use Illuminate\Support\Facades\DB;

class WeChatWithholdCallBack
{
    /**
     * 处理微信签约
     * @return string
     */
    public function handle()
    {

        $array_data = $this->handlerResponse('微信免密支付申请签约回调');
        $is_handle  = DB::table('t_player_info')
            ->where('wx_contract_id', $array_data['contract_id'])
            ->where('wx_contract_expired_time', '>', date('Y-m-d H:i:s'))
            ->value('id');
        if ($is_handle) {
            return WXOrderLogic::instance()->arrayToXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
        }
        if ($array_data['SUCCESS']) {
            $update = ['wx_contract_expired_time' => $array_data['wx_contract_expired_time'], 'wx_withhold_status' => 1];
        } else {
            $update = ['wx_withhold_status' => 2];
            echo WXOrderLogic::instance()->arrayToXml(['return_code' => 'FAIL', 'return_msg' => '响应状态为失败']);
        }
        if (!DB::table('t_player_info')->where('wx_contract_id', $array_data['contract_id'])->update($update)) {
            Logger::instance()->error('微信免密支付申请签约回调', '更新数据库失败');
        } else {
            Logger::instance()->error('微信免密支付申请签约回调', '更新数据库成功');
        }
    }


    /**
     * 处理微信解约
     * @return string
     */
    public function deleteContract()
    {
        $array_data = $this->handlerResponse('微信免密支付解约回调');
        if ($array_data['SUCCESS']) {
            if (!DB::table('t_player_info')->where('wx_contract_id', $array_data['contract_id'])->update([
                'wx_contract_id' => '',
                'wx_contract_expired_time' => null,
                'wx_withhold_status' => 0
            ])) {
                Logger::instance()->error('微信免密支付解约回调', '更新数据库失败');
            } else {
                Logger::instance()->error('微信免密支付解约回调', '更新数据库成功');
            }
            return WXOrderLogic::instance()->arrayToXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
        } else {
            return WXOrderLogic::instance()->arrayToXml(['return_code' => 'FAIL', 'return_msg' => '响应状态为失败']);
        }
    }


    private function handlerResponse($msg)
    {
        $xml = file_get_contents('php://input');

        Logger::instance()->info($msg, "\n" . $xml . "\n\n");

        return json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
    }
}